package restlet.runtime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.restlet.Restlet;
import org.restlet.data.MediaType;
import org.restlet.resource.Resource;
import org.restlet.resource.Variant;

import restlet.handlers.DeleteHandler;
import restlet.handlers.GetHandler;
import restlet.handlers.PostHandler;
import restlet.handlers.PutHandler;

public class RESTResource {

	protected String URI;
	protected Restlet resourceHandler;
	protected List<Variant> variants = new ArrayList<Variant>();
	protected List<MediaType> mediaTypes = new ArrayList<MediaType>();
	protected Map<MediaType, GetHandler> getHandlers = new HashMap<MediaType, GetHandler>();
	protected Map<MediaType, PostHandler> postHandlers = new HashMap<MediaType, PostHandler>();
	protected Map<MediaType, PutHandler> putHandlers = new HashMap<MediaType, PutHandler>();
	protected DeleteHandler deleteHandler;
	private boolean modifiable;
	private boolean readable;

	public void configure(CustomResource resource) {
		resource.getVariants().addAll(variants);
		resource.setModifiable(modifiable);
		resource.setReadable(readable);
	}

	public void compileMediaTypes() {
		variants.clear();

		mediaTypes.addAll(getHandlers.keySet());
		mediaTypes.addAll(postHandlers.keySet());
		mediaTypes.addAll(putHandlers.keySet());

		for (MediaType mediaType : mediaTypes) {
			variants.add(new Variant(mediaType));
		}

		if (postHandlers.size() > 0 || putHandlers.size() > 0
				|| deleteHandler != null)
			modifiable = true;
		if (getHandlers.size() > 0)
			readable = true;
	}

	public Map<MediaType, GetHandler> getGetHandlers() {
		return getHandlers;
	}

	public void setGetHandlers(Map<MediaType, GetHandler> getHandlers) {
		this.getHandlers = getHandlers;
	}

	public Map<MediaType, PostHandler> getPostHandlers() {
		return postHandlers;
	}

	public void setPostHandlers(Map<MediaType, PostHandler> postHandlers) {
		this.postHandlers = postHandlers;
	}

	public Map<MediaType, PutHandler> getPutHandlers() {
		return putHandlers;
	}

	public void setPutHandlers(Map<MediaType, PutHandler> putHandlers) {
		this.putHandlers = putHandlers;
	}

	public DeleteHandler getDeleteHandler() {
		return deleteHandler;
	}

	public void setDeleteHandler(DeleteHandler deleteHandler) {
		this.deleteHandler = deleteHandler;
	}

	public String getURI() {
		return URI;
	}

	public void setURI(String uri) {
		URI = uri;
	}

	public Restlet getResourceHandler() {
		return resourceHandler;
	}

	public void setResourceHandler(Restlet resource) {
		this.resourceHandler = resource;
	}

}
